import type { $RuleItem } from '@/mxp-ui/mxp-form/mxp-form.vue';
import { password_RegEx, tel_RegEx } from '@/mxp-ui/mxp-tool/utils/verify';

/**
 * 得到密码验证规则
 */
export function getPasswordRules() {
  const passwordMin = 8;
  const passwordMax = 24;

  const passwordRules = (text: '原密码' | '密码' | '新密码' | '确认密码'): $RuleItem[] => {
    const pssdErrText = `${text}至少包含字母、数字、特殊字符中的两种，且不能出现4个大小连续或相同的数字`;
    return [
      { required: true, type: 'string', message: `请输入${text}`, trigger: ['blur', 'change'] },
      { min: passwordMin, max: passwordMax, type: 'string', message: `${text}长度在 ${passwordMin} 到 ${passwordMax} 之间`, trigger: ['blur', 'change'] },
      { pattern: password_RegEx, type: 'string', message: pssdErrText, trigger: ['blur', 'change'] },
    ];
  };
  return { passwordMin, passwordMax, passwordRules };
}

/** 得到电话验证规则 */
export function getPhoneRules() {
  const phoneRules: $RuleItem[] = [
    { required: true, type: 'string', message: '请输入手机号', trigger: ['blur', 'change'] },
    { pattern: tel_RegEx, type: 'string', message: '手机号不合法', trigger: ['blur', 'change'] },
  ];
  return { phoneRules };
}

/** 得到账号验证规则 */
export function getUsernameRules() {
  const usernameMin = 6;
  const usernameMax = 24;
  const usernameRules: $RuleItem[] = [
    { required: true, type: 'string', message: '请输入账号', trigger: ['blur', 'change'] },
    { min: usernameMin, max: usernameMax, type: 'string', message: `账号长度在 ${usernameMin} 到 ${usernameMax} 之间`, trigger: ['blur', 'change'] },
    { pattern: /^[a-z0-9A-Z]+$/, type: 'string', message: '账号只能由字母或数字组成', trigger: ['blur', 'change'] },
  ];
  return { usernameRules, usernameMin, usernameMax };
}

/** 得到验证码验证规则 */
export function getSmsCodeRules() {
  const smsCodeLen = 6;
  const smsCodeRules: $RuleItem[] = [
    { required: true, type: 'string', message: '请输入验证码', trigger: ['blur', 'change'] },
    { len: smsCodeLen, type: 'string', message: `验证码长度只能是${smsCodeLen}位`, trigger: ['blur', 'change'] },
    { pattern: /^[0-9]+$/, type: 'string', message: '验证码应全部为数字', trigger: ['blur', 'change'] },
  ];
  return { smsCodeLen, smsCodeRules };
}
